﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Growl.Connector;

namespace GrowlWebBridge
{
    public partial class _Default : System.Web.UI.Page
    {
        //http://192.168.1.135/GrowlWebBridge/default.aspx?Title=Thermostat&Text=Mode+Change+To:++Heat&App=VeraNotify

        private GrowlConnector growl;
        private NotificationType notificationType;
        private Growl.Connector.Application application;
        private string notificationTitle; //Device
        private string notificationText; //Something is up, down, armed,etc
        private string notificationApp; //VeraNotify

        protected void Page_Load(object sender, EventArgs e)
        {
            notificationTitle = Request.QueryString["Title"]; //Device
            notificationText = Request.QueryString["Text"]; //Something is up, down, armed, etc
            notificationApp = Request.QueryString["App"]; //VeraNotify
            

            this.notificationType = new NotificationType("GROWL_EVENT", "Growl Event");

            this.growl = new GrowlConnector();
            //this.growl = new GrowlConnector("password");    // use this if you need to set a password - you can also pass null or an empty string to this constructor to use no password
            //this.growl = new GrowlConnector("password", "hostname", GrowlConnector.TCP_PORT);   // use this if you want to connect to a remote Growl instance on another machine

            this.growl.NotificationCallback += new GrowlConnector.CallbackEventHandler(growl_NotificationCallback);

            // set this so messages are sent in plain text (easier for debugging)
            this.growl.EncryptionAlgorithm = Cryptography.SymmetricAlgorithmType.PlainText;


            this.application = new Growl.Connector.Application(notificationApp);

            this.growl.Register(application, new NotificationType[] { notificationType });


            
            CallbackContext callbackContext = new CallbackContext("some fake information", "fake data");



            Notification notification = new Notification(this.application.Name, this.notificationType.Name, DateTime.Now.Ticks.ToString(), notificationTitle, notificationText,null,false,Priority.VeryLow,null);
            this.growl.Notify(notification, callbackContext);
        }



        void growl_NotificationCallback(Response response, CallbackData callbackData, object state)
        {
            string text = String.Format("Response Type: {0}\r\nNotification ID: {1}\r\nCallback Data: {2}\r\nCallback Data Type: {3}\r\n", callbackData.Result, callbackData.NotificationID, callbackData.Data, callbackData.Type);

            //MessageBox.Show(text, "Callback received", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
        }
    }
}
